package com.chenliang.tk.config;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.beans.factory.annotation.Value;
import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

import java.util.Iterator;
import java.util.Set;


@Slf4j
public class ClInsertListProvider extends MapperTemplate {


    private static final String bindTitle = "<bind name=\"listNotEmptyCheck\" value=\"@tk.mybatis.mapper.util.OGNL@notEmptyCollectionCheck(list, '%s方法参数为空')\"/>\n";
    private static final String bindColumn = "<bind name=\"%sGenIdBind\" value=\"@tk.mybatis.mapper.genid.GenIdUtil@genId(";
    private static final String trimContent = "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">";


    @Value("${spring.jdbc.type}")
    private String jdbcType;

    // 构造方法
    public ClInsertListProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 插入数据
     *
     * @param ms
     * @return
     */
    public String insertList(MappedStatement ms) {
        //   String jdbcType = ApplicationPropertiesUtil.getProperty("spring.jdbc.type", "oracle");
        // String jdbcType = "mysql";
//        if (StringUtils.isBlank(jdbcType)){
//            throw new CustomClException("500","请配置文件添加:spring.jdbc.type");
//        }

        return "oracle".equals("jdbcType") ? this.insertListToOracle(ms) : this.insertListToMysql(ms);
    }

    public String insertListToOracle(MappedStatement ms) {
        Class entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(String.format(bindTitle, ms.getId()));
        sql.append("INSERT ALL\n");
        sql.append("<foreach collection=\"list\" item=\"record\">\n");
        String tableName = SqlHelper.getDynamicTableName(entityClass, this.tableName(entityClass));
        String columns = SqlHelper.insertColumns(entityClass, false, false, false);
        sql.append(" INTO ").append(tableName).append(" ").append(columns).append("\n");
        sql.append(" VALUES ");
        sql.append(trimContent);
        Set columnList = EntityHelper.getColumns(entityClass);
        Iterator var7 = columnList.iterator();

        EntityColumn column;
        while (var7.hasNext()) {
            column = (EntityColumn) var7.next();
            if (column.getGenIdClass() != null) {
                StringBuffer bindBuff = new StringBuffer();
                bindBuff.append(String.format(bindColumn, column.getColumn()));
                bindBuff.append("record").append(", '").append(column.getProperty()).append("'");
                bindBuff.append(", @").append(column.getGenIdClass().getCanonicalName()).append("@class");
                bindBuff.append(", '").append(this.tableName(entityClass)).append("'");
                bindBuff.append(", '").append(column.getColumn()).append("')");
                bindBuff.append("\"/>");
                sql.append(SqlHelper.getIfIsNull("record", column, bindBuff.toString(), true));
            }
        }

        var7 = columnList.iterator();

        while (var7.hasNext()) {
            column = (EntityColumn) var7.next();
            if (column.isInsertable()) {
                sql.append(column.getColumnHolder("record") + ",");
            }
        }

        sql.append("</trim>\n");
        sql.append("</foreach>\n");
        sql.append("SELECT 1 FROM DUAL");
        log.info("ClInsertListProvider.insertListToOracle 执行的sql:{}",sql.toString());
        return sql.toString();
    }

    public String insertListToMysql(MappedStatement ms) {
        Class entityClass = this.getEntityClass(ms);
        StringBuilder sql = new StringBuilder();
        sql.append(String.format(bindTitle, ms.getId()));
        sql.append(SqlHelper.insertIntoTable(entityClass, this.tableName(entityClass)));
        sql.append(SqlHelper.insertColumns(entityClass, false, false, false));
        sql.append(" VALUES ");
        sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
        sql.append(trimContent);
        Set columnList = EntityHelper.getColumns(entityClass);
        Iterator var5 = columnList.iterator();

        EntityColumn column;
        while (var5.hasNext()) {
            column = (EntityColumn) var5.next();
            if (column.getGenIdClass() != null) {
                sql.append(String.format(bindColumn, column.getColumn()));
                sql.append("record").append(", '").append(column.getProperty()).append("'");
                sql.append(", @").append(column.getGenIdClass().getCanonicalName()).append("@class");
                sql.append(", '").append(this.tableName(entityClass)).append("'");
                sql.append(", '").append(column.getColumn()).append("')");
                sql.append("\"/>");
            }
        }

        var5 = columnList.iterator();

        while (var5.hasNext()) {
            column = (EntityColumn) var5.next();
            if (column.isInsertable()) {
                sql.append(column.getColumnHolder("record") + ",");
            }
        }

        sql.append("</trim>");
        sql.append("</foreach>");
        log.info("ClInsertListProvider.insertListToMysql 执行的sql:{}",sql.toString());
        return sql.toString();
    }
}
